package com.jimmy.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @ ClassName MyAdvice
 * <p> TODO
 * @ author JIMMY
 * @ Date 2020/10/31 0031 下午 10:04
 */
@Component
@Aspect
public class MyAdvice {

//    在任意方法上方声明该方法对应一个切入点
//    @Pointcut("execution(* cn.itcast.aop.annotation.UserImpl.add())")
//    pivate void pt1(){}
//    调用格式：类名.切入点方法()
//    @Before("MyAdvice.pt1()")
//AOP注解式开发通知顺序
//        总体顺序由上到下为下列描述顺序 (和XML配置不同，XML配置顺序相关)
//•around before
//•before
//•around after
//•after
//•afterReturning


//   AOP 测试参数
    @Pointcut("execution(void com.jimmy.aop.UserDao.saveArgs(..))")
    public void pcargs(){
    }
//    测试AOP返回值
    @Pointcut("execution(* com.jimmy.aop.UserDao.saveReturn(..))")
    public void pcreturns(){
    }
    @Before("MyAdvice.pcargs()")
    public void before(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("获取的参数为"+ args[0]+"=="+ args[1]);
        System.out.println("before");
    }
    @After("execution(void com.jimmy.aop.UserDao.save())")
    public void after(){
        System.out.println("after");
    }
    @AfterReturning(value = "MyAdvice.pcreturns()",returning = "abc")
    public void afterReturning(JoinPoint joinPoint,Object abc){
        System.out.println("返回值为"+abc);
        System.out.println("afterReturning");
    }
    @Around("execution(void com.jimmy.aop.UserDao.save())")
    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("aroung before");
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("around after");
    }
}
